//
//  GHPicTableviewCell.m
//  VideoPic
//
//  Created by GH on 16/3/28.
//  Copyright © 2016年 apple. All rights reserved.
//

#import "GHPicTableviewCell.h"
#import "GHPicGroupModel.h"
#import <YYWebImage.h>
#import "GHPushVC.h"
#import "HomeViewController.h"
#import "GHPicSeleVc.h"
#import "GHPicComment.h"


@interface GHPicTableviewCell ()<UIGestureRecognizerDelegate>
@property (nonatomic ,retain)UILabel *labelOfUserName;
@property (nonatomic ,retain)UILabel *labelContect;
@property (nonatomic ,retain)UILabel * labelOfCap;
@property (nonatomic ,retain)UIImageView *imageOfHead;
@property (nonatomic ,retain)GHPicGroupModel *model;
@property (nonatomic ,retain)UIImageView *gifImage;
@property (nonatomic ,retain)UIImageView *picImage;
@property (nonatomic ,assign)BOOL picClick;
@property (nonatomic ,retain)UIButton *buttonWithDZ;
@property (nonatomic ,retain)UIButton *buttonWithdigg;
@property (nonatomic ,retain)UIButton *buttonComm;
@property (nonatomic ,retain)UIButton *buttonShare;
@property (nonatomic ,retain)UILabel *labelWithDZ;
@property (nonatomic ,retain)UILabel *labelWithdigg;
@property (nonatomic ,retain)UILabel *labelOfComm;
@property (nonatomic ,retain)UILabel *labelOfshare;

@end

@implementation GHPicTableviewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self createLabel];
        [self createImage];
        [self clickButton];
    }

    return self;
}



- (void)clickButton {
    
    self.buttonWithDZ = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.contentView addSubview:self.buttonWithDZ];
    
    self.buttonWithdigg = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.contentView addSubview:self.buttonWithdigg];
    
    self.buttonComm = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.contentView addSubview:self.buttonComm];
    
    self.buttonShare = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.contentView addSubview:self.buttonShare];
    
}




- (void)createImage {

    self.imageOfHead = [[UIImageView alloc] init];
    [self.contentView addSubview:self.imageOfHead];
    self.gifImage = [YYAnimatedImageView new];
    [self.contentView addSubview:self.gifImage];
    self.gifImage.clipsToBounds = YES;
    
   
    self.picImage = [[UIImageView alloc] init];
    [self.contentView addSubview:self.picImage];
    self.picImage.userInteractionEnabled = YES;
    self.picImage.clipsToBounds = YES;

    
    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleClick:)];
    [self.picImage addGestureRecognizer:tapGesture];
    tapGesture.numberOfTouchesRequired = 1;
    tapGesture.delegate = self;
 
}

- (void)createLabel {

    self.labelOfUserName = [[UILabel alloc] init];
    [self.contentView addSubview:self.labelOfUserName];
    
    self.labelContect = [[UILabel alloc] init];
    [self.contentView addSubview:self.labelContect];
    self.labelContect.numberOfLines = 0;
    
    self.labelOfCap = [[UILabel alloc] init];
    [self.contentView addSubview:self.labelOfCap];
    self.labelOfCap.font = [UIFont systemFontOfSize:12];
    

}
- (void)passModelWith:(GHPicGroupModel *)model{
    
    
    self.model = model;
//    self.model.r_height = @([UIScreen mainScreen].bounds.size.width *self.model.r_height.floatValue / self.model.r_width.floatValue);
    
    
    self.labelOfUserName.text = model.name;
    self.imageOfHead.yy_imageURL = [NSURL URLWithString:model.avatar_url];
    self.labelContect.text = model.content;
    self.labelContect.numberOfLines = 0;
    self.labelOfCap.text = model.category_name;
    
    
    NSLog(@"Modelheight:  %@",self.model.r_height);
//gif
    
    self.gifImage.yy_imageURL = [NSURL URLWithString:model.gifUrl];
    self.picImage.yy_imageURL = [NSURL URLWithString:model.picUrl];
    

    

}

- (void)handleClick:(UITapGestureRecognizer *)tap {
    if ([self.model.media_type isEqual:@1]) {
        
    
    
    id result = [self nextResponder];
    while (![result isKindOfClass:[HomeViewController class]] && result != nil) {
        result = [result nextResponder];
    }
    
    HomeViewController *vc =  (HomeViewController *)result;
    GHPushVC *push = [[GHPushVC alloc] init];
    
    vc.hidesBottomBarWhenPushed = YES;
    vc.navigationController.navigationBar.hidden = YES;
  
    
    [[NSNotificationCenter defaultCenter] addObserverForName:@"tabBarhidden" object:nil queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification * _Nonnull note) {
        vc.hidesBottomBarWhenPushed = NO;
    }];
    
    CGRect frame = [self.picImage convertRect:[UIScreen mainScreen].bounds toView:0];

    
    push.x = frame.origin.x;
    push.y = frame.origin.y;
    push.url = self.model.picUrl;
    push.height = self.model.r_height.integerValue;
    push.heightNew = self.model.r_height.integerValue;

    
    
    [vc.navigationController pushViewController:push animated:NO];
    }
}



- (void)layoutSubviews {
    [super layoutSubviews];
    
    //头像
    self.imageOfHead.frame = CGRectMake(20, 20, SCREEN_WIDTH / 10, SCREEN_WIDTH / 10);
    self.imageOfHead.layer.masksToBounds = YES;
    self.imageOfHead.layer.cornerRadius = SCREEN_WIDTH / 20;
    
    //用户名
    self.labelOfUserName.frame = CGRectMake(30 + SCREEN_WIDTH / 10, 20 + SCREEN_WIDTH/ 20 - 13, SCREEN_WIDTH/2, 25);
    self.labelOfUserName.font = [UIFont systemFontOfSize:13];
    
    //title
    self.labelContect.frame = CGRectMake(20, 20 + SCREEN_WIDTH /10 +15, SCREEN_WIDTH  - 40, [self heightForLabel:self.labelContect.text]);
    CGFloat titleBom = [self heightForLabel:self.labelContect.text] + 35 + SCREEN_WIDTH / 10;
    
    CGFloat userHeight;
    
    //cap
    self.labelOfCap.numberOfLines = 0;
    if (self.labelContect == nil) {
        self.labelOfCap.frame = CGRectMake(20, titleBom + 15 - 20, [self heightForLabel:self.labelOfCap.text], 20);
        userHeight = titleBom + 25;
    }else {
        self.labelOfCap.frame = CGRectMake(20, titleBom + 15, 100, 20);
        userHeight = titleBom + 40;
    }
    self.labelOfCap.textAlignment = 1;
    self.labelOfCap.layer.borderWidth = 1;
    self.labelOfCap.layer.borderColor = [UIColor blackColor].CGColor;
    self.labelOfCap.layer.masksToBounds = YES;
    self.labelOfCap.layer.cornerRadius = 10;
    
       CGFloat height = @([UIScreen mainScreen].bounds.size.width *self.model.r_height.floatValue / self.model.r_width.floatValue).integerValue;

    if (height >500) {
        height = 350;
    }
    NSLog(@"height:--------   %f",height);

    CGFloat all = height + 80 ;
#pragma mark type 1
    
    NSLog(@"%@",self.model.media_type);
    if ([self.model.media_type isEqual:@1]) {
    
        if (height >500) {
            height = 350;
        }

         self.picImage.frame = CGRectMake(20, userHeight+ 20, SCREEN_WIDTH - 40, height);
        NSLog(@"%@",self.model.picUrl);
        all = all + 90 ;
    }
    
    
    
    
#pragma mark type 2
    if ([self.model.media_type  isEqual: @2]) {
        
        if (height >500) {
            height = 350;
        }
        self.gifImage.frame = CGRectMake(20, userHeight, SCREEN_WIDTH - 40, height);
        all = all + 90;

    }
    
    self.buttonWithDZ.frame = CGRectMake(20, all , 20, 20);
    [self.buttonWithDZ setImage:[UIImage imageNamed:@"zan"] forState:UIControlStateNormal];
    
    self.buttonWithdigg.frame = CGRectMake(SCREEN_WIDTH / 3 - 10, all , 20, 20);
    [self.buttonWithdigg setImage:[UIImage imageNamed:@"bad"] forState:UIControlStateNormal];
    
    
    //buttonCom
    self.buttonComm.frame = CGRectMake(SCREEN_WIDTH /3 *2 - 40, all , 20, 20);
    [self.buttonComm setImage:[UIImage imageNamed:@"pinglun"] forState:UIControlStateNormal];
    
    //分享
    self.buttonShare.frame = CGRectMake(SCREEN_WIDTH - 80, all , 20, 20);
    [self.buttonShare setImage:[UIImage imageNamed:@"share"] forState:UIControlStateNormal];
    

    
}



-(CGFloat)heightForLabel:(NSString *)text{
    
    CGRect rect = [text boundingRectWithSize:CGSizeMake(SCREEN_WIDTH - 40, 0) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName : [UIFont systemFontOfSize:17]} context:nil];
    
    
    return rect.size.height;
}

+ (CGFloat)heightForLabel:(NSString *)text{
    
    CGRect rect = [text boundingRectWithSize:CGSizeMake([UIScreen mainScreen].bounds.size.width - 40, 0) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName : [UIFont systemFontOfSize:17]} context:nil];
    
    
    return rect.size.height;
}


+ (CGFloat)heightForCellWith:(GHPicGroupModel *)model {

    CGFloat height;
//    @([UIScreen mainScreen].bounds.size.width *self.model.r_height.floatValue / self.model.r_width.floatValue).integerValue
    CGFloat modelHeight = @([UIScreen mainScreen].bounds.size.width *model.r_height.floatValue / model.r_width.floatValue).integerValue;

    if (modelHeight > 500) {
        modelHeight = 350;
    }
    
    height = [[self class] heightForLabel:model.content] + 35 +[UIScreen mainScreen].bounds.size.width / 10;

    if (model.content == nil) {
        
        height = height + 25;
    }else {
        height = height + 40;
    }
    height = height + 20 + 80 + modelHeight ;
    

    
    return height;
}



//
//- (void)prepareForReuse
//{
//    [super prepareForReuse];
//}
//


- (BOOL)gestureRecognizer:(UIGestureRecognizer*)gestureRecognizer shouldReceiveTouch:(UITouch *)touch{
//    if ([touch.view isKindOfClass:[UIScrollView class]]){
//        return NO;
//    }
//    // 若为UITableViewCellContentView（即点击了tableViewCell），则不截获Touch事件
//    if ([NSStringFromClass([touch.view class])isEqualToString:@"UITableViewCellContentView"]) {
//        return NO;
//    }
    return YES;
}


- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
